// 登录
// import third from '@/sheep/api/third';
import SocialApi from '@/sheep/api/member/social';

// TODO: 等后面搞 App 再弄
const socialType: number = 32; // 社交类型 - 微信开放平台

const load = async (): Promise<void> => {};

// 微信开放平台移动应用授权登陆
const login = (): Promise<boolean> => {
  return new Promise<boolean>(async (resolve, reject) => {
    const loginRes = await uni.login({
      provider: 'weixin',
      onlyAuthorize: true,
    });
    debugger;
    if (loginRes.errMsg === 'login:ok') {
      // const res = await third.wechat.login({
      //   platform: 'openPlatform',
      //   shareInfo: uni.getStorageSync('shareLog') || {},
      //   payload: encodeURIComponent(
      //     JSON.stringify({
      //       code: loginRes.code,
      //     }),
      //   ),
      // });

      // TODO: Uncomment the above code when the 'third' module is implemented
      // if (res.error === 0) {
      //   resolve(true);
      // }
      // Use mock implementation for now
      resolve(true);
    } else {
      uni.showToast({
        icon: 'none',
        title: loginRes.errMsg,
      });
    }
    resolve(false);
  });
};

// 微信 App 解除绑定
const unbind = async (openid: string): Promise<boolean> => {
  const { code } = await SocialApi.socialUnbind(socialType, openid);
  return code === 0;
};

// 获得社交信息
const getInfo = async (): Promise<SocialInfo | undefined> => {
  const { code, data } = await SocialApi.getSocialUser(socialType);
  if (code !== 0) {
    return undefined;
  }
  return data;
};

export default {
  load,
  login,
  getInfo,
};

interface SocialInfo {
  // Define properties of SocialInfo interface here
}
